//收入统计
var str;
var sdata=[];
var rdata=[];
var target;
var text;
var name;
var time;
var daytime=[];
var date=new Date;  
var year=date.getFullYear();   
var month=date.getMonth()+1;
var day=date.getDate();
var dateTime=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
$(function () {
	$(".day").val(dateTime);
	time=$(".day").val();
	daytime=time.split("-");//分割时间
	year=daytime[0];
	month=daytime[1];
	str="day";
	data(str,time);
	target=$("#day_income");
	text='每日总收入';
	name=year+'-'+month;
	tu(sdata,rdata,target,text,name);
	//获取数据
	function data(str,time){
		sdata=[];
		rdata=[];
		$.ajax({
			   type: "post",
			   url: "income",
			   data:{  
	                "str":str,
	                "time":time
	            },
			   async: false,
			   success: function(map){
		            for (var prop in map){
		            	sdata.push(prop);
		            	rdata.push(map[prop]);
		            }
			   }
			});
	}
	
	//用于形成统计图片的方法
	function tu(sdata,rdata,target,text,name){
		target.highcharts({
	        chart: {
	            type: 'column'
	        },
	        title: {
	            text: text
	        },
	        subtitle: {
	            text: '数据来源: wumingzuche.houtai'
	        },
	        xAxis: {
	            categories: sdata,
	            crosshair: true
	        },
	        yAxis: {
	            min: 0,
	            title: {
	                text: '收入 (元)'
	            }
	        },
	        tooltip: {
	            headerFormat: '<span style="font-size:10px">    {point.key}</span><table>',
	            pointFormat: '<tr>' +
	            '<td style="padding:0"><b>{point.y:.1f} 元</b></td></tr>',
	            footerFormat: '</table>',
	            shared: true,
	            useHTML: true
	        },
	        plotOptions: {
	            column: {
	                pointPadding: 0.2,
	                borderWidth: 0
	            }
	        },
	        series: [{
	            name: name,
	            data: rdata
	        }]
	    });
	}
	//按月显示
	$("#month").on("click",function(){
		daytime=[];
		var time=$(".day").val();
		daytime=time.split("-");//分割时间
		year=daytime[0];
		$("#day_income").css("display", "none");
		$("#year_income").css("display", "none");
		$("#month_income").css("display", "block");
		str="month";
		data(str,time);
		text='每月总收入';
		name=year;
		target=$("#month_income");
		tu(sdata,rdata,target,text,name);
	});
	//按年显示
	$("#year").on("click",function(){
		daytime=[];
		var time=$(".day").val();
		$("#day_income").css("display", "none");
		$("#month_income").css("display", "none");
		$("#year_income").css("display", "block");
		str="year";
		data(str,time);
		target=$("#year_income");
		text='每年总收入';
		name="";
		tu(sdata,rdata,target,text,name);
	});
	//按天显示
	$("#day").on("click",function(){
		daytime=[];
		var time=$(".day").val();
		daytime=time.split("-");//分割时间
		year=daytime[0];
		month=daytime[1];
		$("#month_income").css("display", "none");
		$("#year_income").css("display", "none");
		$("#day_income").css("display", "block");
		str="day";
		data(str,time);
		target=$("#day_income");
		text='每日总收入';
		name=year+'-'+month;
		tu(sdata,rdata,target,text,name);
	});
	
});
   